package com.example.expresscheck.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.expresscheck.entity.Express;

public class ExpressSeachHostoryDao {
	ExpressSeachHostoryDBHelper dbHelper;

	public ExpressSeachHostoryDao(Context context) {
		dbHelper = new ExpressSeachHostoryDBHelper(context);

	}

	// String sql =
	// "insert into table_ExpressSeachHostory (express_company,express_number) values ('"
	// + express_company + "','" + express_Number + "')";
	// db.execSQL(sql);
	public void add(String express_company, String express_Number) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"insert into table_ExpressSeachHostory (express_company,express_number) values (?,?)",
				new Object[] { express_company, express_Number });
		db.close();
	}

	public Express find(String express_company, String express_number) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select * from table_ExpressSeachHostory where express_company=? and express_number=?",
						new String[] { express_company, express_number });
		if(cursor.moveToFirst()){
			String remark = cursor.getString(cursor.getColumnIndex("remark"));
			int isListen = cursor.getInt(cursor.getColumnIndex("isListen"));
			db.close();
			return new Express(express_company, express_number, remark, isListen);
		}
		db.close();
		return null;
	}

	public void updateRemark(String express_company, String express_Number,
			String remark) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"update table_ExpressSeachHostory set remark =?  where express_company = ? and express_number=?",
				new Object[] { remark, express_company, express_Number });
		db.close();
	}

	public void updateIsListen(String express_company, String express_Number,
			int isListen) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"update table_ExpressSeachHostory set isListen =?  where express_company = ? and express_number=?",
				new Object[] { isListen, express_company, express_Number });
		db.close();
	}

	public void delete(String express_company, String express_Number) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"delete from table_ExpressSeachHostory where express_company = ? and express_number=?",
				new Object[] { express_company, express_Number });
		db.close();
	}

	public List<Express> getListExpress() {
		List<Express> list = new ArrayList<Express>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from table_ExpressSeachHostory",null);
		while(cursor.moveToNext()){
			String express_company = cursor.getString(cursor.getColumnIndex("express_company"));
			String express_number = cursor.getString(cursor.getColumnIndex("express_number"));
			String remark = cursor.getString(cursor.getColumnIndex("remark"));
			int isListen = cursor.getInt(cursor.getColumnIndex("isListen"));
			list.add(new Express(express_company, express_number, remark, isListen));
		}
		cursor.close();
		db.close();
		return list;

	}

	public void updateLastDynamic(String express_company, String express_Number,String lastDynamic) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"update table_ExpressSeachHostory set lastDynamic =?  where express_company = ? and express_number=?",
				new Object[] { lastDynamic, express_company, express_Number });
		db.close();
	}

	public void updateInfo(String express_company, String express_Number,
			String expressInfoStr) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL(
				"update table_ExpressSeachHostory set info =?  where express_company = ? and express_number=?",
				new Object[] { expressInfoStr, express_company, express_Number });
		db.close();
	}

	public List<Express> getListenList() {
		List<Express> list = new ArrayList<Express>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from table_ExpressSeachHostory where isListen = 1",null);
		while(cursor.moveToNext()){
			String express_company = cursor.getString(cursor.getColumnIndex("express_company"));
			String express_number = cursor.getString(cursor.getColumnIndex("express_number"));
			String remark = cursor.getString(cursor.getColumnIndex("remark"));
			int isListen = cursor.getInt(cursor.getColumnIndex("isListen"));
			String lastDynamic  = cursor.getString(cursor.getColumnIndex("lastDynamic"));
			list.add(new Express(express_company, express_number, remark, isListen,lastDynamic));
		}
		cursor.close();
		db.close();
		return list;
	}
}